﻿using System;
using System.IO;

namespace AiMoBridge.Paths
{
    public static class PathManager
    {
        /// <summary>
        /// Roaming/AimoRhino/
        /// </summary>
        public static string MainFolder { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "AimoRhino");
        /// <summary>
        /// 储存本地库
        /// </summary>
        public static string ScriptsFolder { get; } = Path.Combine(MainFolder, "Scripts");
        public static string CustomizedCommandsFolder { get; } = Path.Combine(MainFolder, "User Commands");
        /// <summary>
        /// Info
        /// </summary>
        public static string InfoTxt { get; } = Path.Combine(MainFolder, "Aimo.info");
        /// <summary>
        /// 用来放Debug的Log文件
        /// </summary>
        public static string DebugFolder { get; } = Path.Combine(MainFolder, "Debug");
        public static string TempDllFolder { get; } = Path.Combine(DebugFolder, "CompileCaches");
        public static string GetTempCSPath()
        {
            if (!Directory.Exists(TempDllFolder)) Directory.CreateDirectory(TempDllFolder);
            return Path.Combine(TempDllFolder, Guid.NewGuid().ToString() + ".cs");
        }

        public static void GetTempDllPath(out string fileName, out string dllPath, out string pdbPath)
        {
            fileName = Guid.NewGuid().ToString();
            if (!Directory.Exists(TempDllFolder)) Directory.CreateDirectory(TempDllFolder);
            dllPath = Path.Combine(TempDllFolder, fileName + ".dll");
            pdbPath = Path.Combine(TempDllFolder, fileName + ".pdb");

        }
        public static void ClearCaches()
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(DebugFolder);
            if (directoryInfo.Exists)
            {
                foreach (FileInfo info in directoryInfo.GetFiles())
                {
                    if ((DateTime.Now - info.CreationTime).TotalDays > 1)
                    {
                        try
                        {
                            info.Delete();
                        }
                        catch
                        {
                        }
                    }
                }
            }

            directoryInfo = new DirectoryInfo(TempDllFolder);
            if (directoryInfo.Exists)
            {
                foreach (FileInfo info in directoryInfo.GetFiles())
                {
                    if ((DateTime.Now - info.CreationTime).TotalDays > 1)
                    {
                        try
                        {
                            info.Delete();
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }
    }
}
